package com.wing.api.app;


import com.alibaba.fastjson.JSONObject;
import com.wing.api.add.cellInventoryOut.BaseCellInventoryOutSeriesService;
import com.wing.api.add.cellInventoryOut.CellInventoryOutServiceType;
import com.wing.api.add.company.BaseCompanyService;
import com.wing.api.add.company.CompanyServiceType;
import com.wing.api.add.department.BaseDepartmentService;
import com.wing.api.add.department.DepartmentServiceType;
import com.wing.api.add.device.BaseDeviceService;
import com.wing.api.add.device.DeviceServiceType;
import com.wing.api.add.processInfo.BaseProcessInfoService;
import com.wing.api.add.processInfo.ProcessInfoServiceType;
import com.wing.api.add.productDetail.BaseProductDetailSeriesService;
import com.wing.api.add.productDetail.ProductDetailServiceType;
import com.wing.api.add.productSeries.BaseProductSeriesService;
import com.wing.api.add.productSeries.ProductSeriesServiceType;
import com.wing.api.add.role.BaseRoleService;
import com.wing.api.add.role.RoleServiceType;
import com.wing.api.add.simpleWorkOrder.BaseSimpleWorkOrderService;
import com.wing.api.add.simpleWorkOrder.SimpleWorkOrderServiceType;
import com.wing.api.add.storesRequisition.BaseStoresRequisitionSeriesService;
import com.wing.api.add.storesRequisition.StoresRequisitionServiceType;
import com.wing.api.add.user.BaseUserService;
import com.wing.api.add.user.UserServiceType;
import com.wing.cabinet.model.entity.StoresRequisition;
import com.wing.cabinet.model.response.CellInventoryInoutApiVO;
import com.wing.cabinet.model.response.EnterpricceIdVO;
import com.wing.cabinet.service.EnterpricceIdService;
import com.wing.common.utils.JsonResult;
import com.wing.enterprise.device.request.DeviceForm;
import com.wing.enterprise.feign.EnterpriseClient;
import com.wing.enterprise.product.model.request.ProcessInfoForm;
import com.wing.production.feign.ProductionClient;
import com.wing.production.workOrder.request.SimpleWorkOrderForm;
import com.wing.user.feign.UserClient;
import com.wing.user.model.response.UserVO;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "App-API", tags = "刀柜API")
@RestController("appCabApiControlle")
@RequestMapping("api/app")
@Slf4j
public class ApiController {

    @Autowired
    private EnterpricceIdService enterpricceIdService;
    @Autowired
    private UserClient userClient;
    @Autowired
    private EnterpriseClient enterpriseClient;
    @Autowired
    private ProductionClient productionClient;

    /**
     *  人员同步接口
     * @return
     */
    @PostMapping("/addUser")
    public JsonResult addUser(@RequestBody JSONObject jsonObject) {
        try {
            EnterpricceIdVO enterpricceIdVo = enterpricceIdService.getEnterpricceIdVO();
            String type = "";
            //enterpricceIdVo.setStandard("0");
            if("0".equals(enterpricceIdVo.getStandard())){
                type = "A99999";
            }else{
                type = enterpricceIdVo.getEnterpriseCode();
            }
            BaseUserService baseUserService = (BaseUserService) Class.forName("com.wing.api.add.user.impl." + UserServiceType.typeMap.get(type)).newInstance();
            baseUserService.logicProcess(jsonObject, enterpricceIdVo);
        }catch (Exception e){
            log.error("人员同步接口异常:", e);
            return JsonResult.success(e.toString());
        }

        return JsonResult.success("同步成功");
    }


    /**
     *  岗位同步接口
     * @return
     */
    @PostMapping("/addRole")
    public JsonResult addRole(@RequestBody JSONObject jsonObject) {
        try {
            EnterpricceIdVO enterpricceIdVo = enterpricceIdService.getEnterpricceIdVO();
            String type = "";
            //enterpricceIdVo.setStandard("0");
            if("0".equals(enterpricceIdVo.getStandard())){
                type = "A99999";
            }else{
                type = enterpricceIdVo.getEnterpriseCode();
            }
            BaseRoleService baseRoleService = (BaseRoleService) Class.forName("com.wing.api.add.role.impl." + RoleServiceType.typeMap.get(type)).newInstance();
            baseRoleService.logicProcess(jsonObject, enterpricceIdVo);
        }catch (Exception e){
            log.error("人员同步接口异常:", e);
            return JsonResult.success(e.toString());
        }

        return JsonResult.success("同步成功");
    }

    /**
     *  部门同步接口
     * @return
     */
    @PostMapping("/addDepartment")
    public JsonResult addDepartment(@RequestBody JSONObject jsonObject) {
        try {
            EnterpricceIdVO enterpricceIdVo = enterpricceIdService.getEnterpricceIdVO();
            //enterpricceIdVo.setStandard("0");
            String type = "";
            if("0".equals(enterpricceIdVo.getStandard())){
                type = "A99999";
            }else{
                type = enterpricceIdVo.getEnterpriseCode();
            }
            BaseDepartmentService baseDepartmentService = (BaseDepartmentService) Class.forName("com.wing.api.add.department.impl." + DepartmentServiceType.typeMap.get(type)).newInstance();
            baseDepartmentService.logicProcess(jsonObject, enterpricceIdVo);
        }catch (Exception e){
            log.error("部门同步接口异常:", e);
            return JsonResult.fail(e.toString());
        }

        return JsonResult.success("同步成功");
    }
    /**
     *  企业信息同步接口
     * @return
     */
    @PostMapping("/addCompany")
    public JsonResult addCompany(@RequestBody JSONObject jsonObject) {
        try {
            EnterpricceIdVO enterpricceIdVo = enterpricceIdService.getEnterpricceIdVO();
            //enterpricceIdVo.setStandard("0");
            String type = "";
            if("0".equals(enterpricceIdVo.getStandard())){
                type = "A99999";
            }else{
                type = enterpricceIdVo.getEnterpriseCode();
            }
            BaseCompanyService baseCompanyService = (BaseCompanyService) Class.forName("com.wing.api.add.company.impl." + CompanyServiceType.typeMap.get(type)).newInstance();
            baseCompanyService.logicProcess(jsonObject, enterpricceIdVo);
        }catch (Exception e){
            log.error("企业同步接口异常:", e);
            return JsonResult.fail(e.toString());
        }

        return JsonResult.success("同步成功");
    }
    /**
     *  规格系列同步接口
     * @return
     */
    @PostMapping("/addProductSeries")
    public JsonResult addProductSeries(@RequestBody JSONObject jsonObject) {
        try {
            EnterpricceIdVO enterpricceIdVo = enterpricceIdService.getEnterpricceIdVO();
            //enterpricceIdVo.setStandard("0");
            String type = "";
            if("0".equals(enterpricceIdVo.getStandard())){
                type = "A99999";
            }else{
                type = enterpricceIdVo.getEnterpriseCode();
            }
            BaseProductSeriesService baseProductSeriesService = (BaseProductSeriesService) Class.forName("com.wing.api.add.productSeries.impl." + ProductSeriesServiceType.typeMap.get(type)).newInstance();
            baseProductSeriesService.logicProcess(jsonObject, enterpricceIdVo);
        }catch (Exception e){
            log.error("规格系列同步接口异常:", e);
            return JsonResult.fail(e.toString());
        }

        return JsonResult.success("同步成功");
    }
    /**
     *  物料同步接口
     * @return
     */
    @PostMapping("/addProductDetail")
    public JsonResult addProductDetail(@RequestBody JSONObject jsonObject) {
        try {
            EnterpricceIdVO enterpricceIdVo = enterpricceIdService.getEnterpricceIdVO();
            //enterpricceIdVo.setStandard("0");
            String type = "";
            if("0".equals(enterpricceIdVo.getStandard())){
                type = "A99999";
            }else{
                type = enterpricceIdVo.getEnterpriseCode();
            }
            BaseProductDetailSeriesService baseProductDetailSeriesService = (BaseProductDetailSeriesService) Class.forName("com.wing.api.add.productDetail.impl." + ProductDetailServiceType.typeMap.get(type)).newInstance();
            baseProductDetailSeriesService.logicProcess(jsonObject, enterpricceIdVo);
        }catch (Exception e){
            log.error("物料同步接口异常:", e);
            return JsonResult.fail(e.toString());
        }
        return JsonResult.success("同步成功");
    }

    /**
     *  查询领新记录列表
     * @return
     */
    @PostMapping("/syncCellInventoryInoutList")
    public JsonResult<List<CellInventoryInoutApiVO>> syncCellInventoryInoutList(@RequestBody JSONObject jsonObject) {
        try {
            EnterpricceIdVO enterpricceIdVo = enterpricceIdService.getEnterpricceIdVO();
            //enterpricceIdVo.setStandard("0");
            String type = "";
            if("0".equals(enterpricceIdVo.getStandard())){
                type = "A99999";
            }else{
                type = enterpricceIdVo.getEnterpriseCode();
            }
            BaseCellInventoryOutSeriesService baseCellInventoryOutSeriesService = (BaseCellInventoryOutSeriesService) Class.forName("com.wing.api.add.cellInventoryOut.impl." + CellInventoryOutServiceType.typeMap.get(type)).newInstance();
            List<CellInventoryInoutApiVO> list = baseCellInventoryOutSeriesService.logicProcess(jsonObject, enterpricceIdVo);
            return JsonResult.success(list);
        }catch (Exception e){
            log.error("领新记录同步接口异常:", e);
            return JsonResult.fail(e.toString());
        }

    }

    /**
     * 每月1号凌晨1点执行一次
     * @return
     */
    @GetMapping("/syncCellInventoryInoutData")
    @Scheduled(cron = "0 0 2 23 * ?")
    //@Scheduled(cron = "0 30 15 * * ?") 每天下午3点30分
    public JsonResult syncCellInventoryInoutData(){
        try {
            //EnterpricceIdVO enterpricceIdVo = enterpricceIdService.getEnterpricceIdVO();
            //enterpricceIdVo.setStandard("1");
            String type = "E0524";
           log.info("执行同步!111");
           BaseCellInventoryOutSeriesService baseCellInventoryOutSeriesService = (BaseCellInventoryOutSeriesService) Class.forName("com.wing.api.add.cellInventoryOut.impl." + CellInventoryOutServiceType.typeMap.get(type)).newInstance();
            baseCellInventoryOutSeriesService.syncCellInventoryInoutData(type);
            return JsonResult.success("领新记录同步成功");
        }catch (Exception e){
            log.error("领新记录同步接口异常:", e);
            return JsonResult.fail(e.toString());
        }
    }

    /**
     * 每天凌晨一点
     * @return
     */
    @GetMapping("/syncUserInfo")
    @Scheduled(cron = "0 0 1 23 * ?")
    public JsonResult syncUserInfo(){
        try { EnterpricceIdVO enterpricceIdVo = enterpricceIdService.getEnterpricceIdVO();
            //enterpricceIdVo.setStandard("1");
            String type = "";
            /*if("0".equals(enterpricceIdVo.getStandard())){
                type = "A99999";
            }else{
                type = enterpricceIdVo.getEnterpriseCode();
            }*/
            type = "E0524";
            log.info("执行同步!111");
            BaseUserService baseUserService = (BaseUserService) Class.forName("com.wing.api.add.user.impl." + UserServiceType.typeMap.get(type)).newInstance();
            baseUserService.syncUserInfo(enterpricceIdVo);
            return JsonResult.success("用户数据同步成功");
        }catch (Exception e){
            log.error("用户数据同步接口异常:", e);
            return JsonResult.fail(e.toString());
        }
    }

    /**
     * 添加领新申请接口
     */
    @PostMapping("/submitStoresRequisition")
    public JsonResult submitStoresRequisition(@RequestBody JSONObject jsonObject) {
        try {
            EnterpricceIdVO enterpricceIdVo = enterpricceIdService.getEnterpricceIdVO();
            String type = "";
            if("0".equals(enterpricceIdVo.getStandard())){
                type = "A99999";
            }else{
                type = enterpricceIdVo.getEnterpriseCode();
            }
            jsonObject.put("status", StoresRequisition.Status.checked); // 已审核
            jsonObject.put("checkStatus", StoresRequisition.CheckStatus.approve); // 审核通过
            jsonObject.put("workOrderWay", StoresRequisition.WorkOrderWay.simple); // 审核通过
            jsonObject.put("enterpriseCode", enterpricceIdVo.getEnterpriseCode()); // 企业编号
            Long userId = Long.parseLong(jsonObject.get("userId").toString());
            JsonResult<UserVO> user = userClient.getUserInfo(userId);
            jsonObject.put("creatorName", user.getData().getNickname() + "|" + user.getData().getUsername()); // 创建人名称，默认为领新人数据
            jsonObject.put("loginUserId", userId); // 默认登录用户ID为领新人ID
            BaseStoresRequisitionSeriesService baseStoresRequisitionSeriesService = (BaseStoresRequisitionSeriesService) Class.forName("com.wing.api.add.storesRequisition.impl." + StoresRequisitionServiceType.typeMap.get(type)).newInstance();
            baseStoresRequisitionSeriesService.logicProcess(jsonObject, enterpricceIdVo);
            return JsonResult.success("操作成功");
        }catch (Exception e){
            log.error("添加领新申请接口异常，原因是：{} ", e.getMessage(), e);
            return JsonResult.fail("操作失败:" + e.getMessage());
        }
    }

    /**
     *  领新接口
     * @return
     */
    @PostMapping("/inventoryIn")
    public JsonResult inventoryIn(@RequestBody JSONObject jsonObject) {
        try {
            EnterpricceIdVO enterpricceIdVo = enterpricceIdService.getEnterpricceIdVO();
            String type = "";
            if("0".equals(enterpricceIdVo.getStandard())){
                type = "A99999";
            }else{
                type = enterpricceIdVo.getEnterpriseCode();
            }
            BaseCellInventoryOutSeriesService baseCellInventoryOutSeriesService = (BaseCellInventoryOutSeriesService) Class.forName("com.wing.api.add.cellInventoryOut.impl." + CellInventoryOutServiceType.typeMap.get(type)).newInstance();
            baseCellInventoryOutSeriesService.inventoryIn(jsonObject);
        }catch (Exception e){
            log.error("操作异常:", e);
            return JsonResult.fail(e.toString());
        }

        return JsonResult.success("操作成功");
    }

    /**
     * 工序同步接口
     * @return
     */
    @PostMapping("/addProcessInfo")
    public JsonResult addProcessInfo(@RequestBody ProcessInfoForm processInfoForm) {
        try {
            EnterpricceIdVO enterpricceIdVo = enterpricceIdService.getEnterpricceIdVO();
            //enterpricceIdVo.setStandard("0");
            String type = "";
            if("0".equals(enterpricceIdVo.getStandard())){
                type = "A99999";
            }else{
                type = enterpricceIdVo.getEnterpriseCode();
            }
            BaseProcessInfoService baseProcessInfoService = (BaseProcessInfoService) Class.forName("com.wing.api.add.processInfo.impl." + ProcessInfoServiceType.typeMap.get(type)).newInstance();
            baseProcessInfoService.logicProcess(processInfoForm, enterpricceIdVo, enterpriseClient);
        }catch (Exception e){
            log.error("工序同步接口异常:", e);
            return JsonResult.fail(e.toString());
        }
        return JsonResult.success("同步成功");
    }

    /**
     * 设备同步接口
     * @return
     */
    @PostMapping("/addDevice")
    public JsonResult addDevice(@RequestBody DeviceForm deviceForm) {
        try {
            EnterpricceIdVO enterpricceIdVo = enterpricceIdService.getEnterpricceIdVO();
            //enterpricceIdVo.setStandard("0");
            String type = "";
            if("0".equals(enterpricceIdVo.getStandard())){
                type = "A99999";
            }else{
                type = enterpricceIdVo.getEnterpriseCode();
            }
            BaseDeviceService baseDeviceService = (BaseDeviceService) Class.forName("com.wing.api.add.device.impl." + DeviceServiceType.typeMap.get(type)).newInstance();
            baseDeviceService.logicProcess(deviceForm, enterpricceIdVo, enterpriseClient);
        }catch (Exception e){
            log.error("设备同步接口异常:", e);
            return JsonResult.fail(e.toString());
        }
        return JsonResult.success("同步成功");
    }

    /**
     * 工单同步接口
     * @return
     */
    @PostMapping("/addSimpleWorkOrder")
    public JsonResult addSimpleWorkOrder(@RequestBody SimpleWorkOrderForm simpleWorkOrderForm) {
        try {
            EnterpricceIdVO enterpricceIdVo = enterpricceIdService.getEnterpricceIdVO();
            //enterpricceIdVo.setStandard("0");
            String type = "";
            if("0".equals(enterpricceIdVo.getStandard())){
                type = "A99999";
            }else{
                type = enterpricceIdVo.getEnterpriseCode();
            }
            BaseSimpleWorkOrderService baseSimpleWorkOrderService = (BaseSimpleWorkOrderService) Class.forName("com.wing.api.add.simpleWorkOrder.impl." + SimpleWorkOrderServiceType.typeMap.get(type)).newInstance();
            baseSimpleWorkOrderService.logicProcess(simpleWorkOrderForm, enterpricceIdVo, productionClient);
        }catch (Exception e){
            log.error("工单同步接口异常:", e);
            return JsonResult.fail(e.toString());
        }
        return JsonResult.success("同步成功");
    }

}


